﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace ServBus.ListForm2010.Controls
{
    public class BreadCrumbNav : WebControl
    {
        private bool _isCentralAdmin = false;
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(false)]
        public bool IsCentralAdmin
        {
            get { return _isCentralAdmin; }
            set { _isCentralAdmin = value; }
        }

        private HtmlGenericControl GenerateSpan(string text)
        {
            HtmlGenericControl span = new HtmlGenericControl("span");
            span.Attributes.Add("class", "ms-sitemapdirectional");
            span.InnerText = text;
            return span;
        }

        private HtmlGenericControl GenerateSperator()
        {
            HtmlGenericControl span = new HtmlGenericControl("span");
            span.InnerText = " > ";
            return span;
        }

        private HtmlGenericControl GenerateLink(string text, string href, string hoverText)
        {
            HtmlGenericControl span = new HtmlGenericControl("span");
            HtmlGenericControl link = new HtmlGenericControl("a");
            link.Attributes.Add("class", "ms-sitemapdirectional");
            link.Attributes.Add("href", href);
            if (!string.IsNullOrEmpty(hoverText))
                link.Attributes.Add("title", hoverText);
            link.InnerText = text;
            span.Controls.Add(link);
            return span;
        }

        private void AddWebTree(SPWeb currentWeb)
        {
            List<SPWeb> webs = new List<SPWeb>();
            SPSecurity.RunWithElevatedPrivileges(() =>
            {
                using (SPSite site = new SPSite(currentWeb.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb(currentWeb.ID))
                    {
                        SPWeb webInNav = web;
                        webs.Add(webInNav);
                        while (webInNav.ParentWeb != null && webInNav.Navigation.UseShared)
                        {
                            webInNav = webInNav.ParentWeb;
                            if (webInNav != null)
                                webs.Add(webInNav);
                        }

                        // Add links
                        Controls.Add(GenerateLink(webs[webs.Count - 1].Title, webs[webs.Count - 1].Url, null));
                        for (int i = webs.Count - 2; i >= 0; i--)
                        {
                            Controls.Add(GenerateSperator());
                            Controls.Add(GenerateLink(webs[i].Title, webs[i].Url, null));
                        }
                    }
                }
            });
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (IsCentralAdmin)
            {
                Controls.Add(GenerateLink(SPResource.GetString(Strings.CentralAdminSiteTitle), "/", null));
                Controls.Add(GenerateSperator());
                Controls.Add(GenerateLink(
                                HttpContext.GetGlobalResourceObject("spadmin", "applications_pagetitle").ToString(),
                                "/_admin/ListForm2010/Management.aspx", null));
            }
            else
            {
                SPWeb currentWeb = SPContext.Current.Web;
                AddWebTree(currentWeb);

                // Add List
                SPList currentList = SPContext.Current.List;
                Controls.Add(GenerateSperator());
                Controls.Add(GenerateLink(currentList.Title, currentList.DefaultView.ServerRelativeUrl, null));

                // Add List Settings
                Controls.Add(GenerateSperator());
                Controls.Add(GenerateLink(
                                HttpContext.GetGlobalResourceObject("wss", "lstsetng_settings").ToString(),
                                currentWeb.Url + "/_layouts/listedit.aspx?List=" + currentList.ID.ToString(), null));
            }
        }

        public void AddLink(string text, string href, string hoverText)
        {
            EnsureChildControls();
            Controls.Add(GenerateSperator());

            if (string.IsNullOrEmpty(href))
            {
                Controls.Add(GenerateSpan(text));
            }
            else
            {
                Controls.Add(GenerateLink(text, href, hoverText));
            }
        }
    }
}
